package com.winit.openapi.chinapost.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.isp.ISPChinaPostOrderService;
import com.winit.oms.spi.isp.command.chinapost.ISPChinaPostClearanceCommand;
import com.winit.openapi.chinapost.constants.ChinaPostErrorCode;
import com.winit.openapi.chinapost.constants.XMLUtils;
import com.winit.openapi.chinapost.model.RequestObject;
import com.winit.openapi.chinapost.model.ResponsesObject;
import com.winit.openapi.chinapost.model.Trace;
import com.winit.openapi.chinapost.model.TracesElement;
import com.winit.openapi.chinapost.model.TracesList;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.Config;

@Controller
@RequestMapping(value = "/CPClearance")
public class ChinaPostClearanceController extends BaseController {
    
    @Resource
    private ISPChinaPostOrderService iSPChinaPostOrderService;
    @Resource
    private Config             config;
    private static final Logger logger = LoggerFactory.getLogger(ChinaPostClearanceController.class);


    @RequestMapping(value = "/API")
    @ResponseBody
    public String doAPI(@ModelAttribute RequestObject requestObject) {
        logger.info("clearanceAPI中邮请求参数-----------》" + JSON.toJSONString(requestObject));
        String errorCode = null;
        try {
            // step 1 校验参数
            verifyParameters(requestObject);
            //setp 2 验证签名 目前清关接口没签名，先注释，作者马健 2016年8月9日10:29:10
//            if(!config.isSignFlag()){
//                verifySignature(requestObject);
//            }
            TracesList tracesList = (TracesList) XMLUtils.clearanceXmlToObject(requestObject.getLogistics_interface());
            List<TracesElement> tracesElement = tracesList.getTracesElement();
            // 设置上下文
            CommandContext ctx = CommandContext.getContext();
            List<ISPChinaPostClearanceCommand> list = new ArrayList<ISPChinaPostClearanceCommand>();
            for (TracesElement e : tracesElement) {
                for (Trace trace : e.getTrace()) {
                    ISPChinaPostClearanceCommand command = new ISPChinaPostClearanceCommand();
                    command.setCtx(ctx);
                    command.setMailNos(e.getMailNos());
                    command.setTxLogisticID(e.getTxLogisticID());
                    command.setCity(trace.getCity());
                    command.setDesc(trace.getDesc());
                    command.setTime(trace.getTime());
                    command.setAction(trace.getAction());
                    list.add(command);
                }
            }
            iSPChinaPostOrderService.addChinaClearance(list);
        }  catch (ApiException e) {
            errorCode = e.getErrorCode();
            logger.info("中邮ApiException:", e);
            return this.responseError(errorCode);
        } catch (Exception e) {
            errorCode = ChinaPostErrorCode.ERROR_S07;
            logger.error("中邮ApiException:", e);
            return this.responseError(errorCode);
        }
        ResponsesObject responsesObject = this.getResponsesObject(errorCode);
        logger.info("clearanceAPIAPI中邮请求返回参数-----------》" + JSON.toJSONString(responsesObject) + ",errorCode: =" + errorCode);
        return this.responseSuccess();
    }

  
}
